/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include ".\mpopupmenu.h"

//---------------------------------------------------------------------------

MPopupMenu::MPopupMenu(void)
{
	isCreated = false;
}

MPopupMenu::~MPopupMenu(void)
{
	CleanUp();
	Destroy();
}

//---------------------------------------------------------------------------

void MPopupMenu::CleanUp(void)
{
	MPopupMenuItem *elem;

	for(itor=theVector.begin(); itor!=theVector.end(); itor++){
		elem = *itor;
		if(elem!=NULL){
			*itor = NULL;
			delete elem;
		}
	}
	theVector.clear();
}

//---------------------------------------------------------------------------

bool MPopupMenu::AddMenuItem(UINT idIn, CString nameIn)
{
	MPopupMenuItem *elem;
	int cnt;
	
	try{
		elem = new MPopupMenuItem;
	}
	catch(...){
		elem = NULL;
	}
	if(elem==NULL) return false;

	elem->xId   = idIn;
	elem->xName = nameIn;

	cnt = (int)theVector.size();
	theVector.push_back(elem);
	cnt = (int)theVector.size() - cnt;
	if(cnt!=1){
		delete elem;
		return false;
	}

	return true;
}

void MPopupMenu::Clear(void)
{
	CleanUp();
}

//---------------------------------------------------------------------------

bool MPopupMenu::Create(void)
{
	MPopupMenuItem *elem;
	bool err;

	Destroy();

	if(!popupMenu.CreatePopupMenu())
		return false;

	err = false;
	for(itor=theVector.begin(); itor!=theVector.end() && !err; itor++){
		elem = *itor;
		if(elem!=NULL){
			if(elem->xName.IsEmpty()){
				if(!popupMenu.AppendMenu(MF_SEPARATOR, 0, _T("")))
					err = true;
			}
			else{
				if(!popupMenu.AppendMenu(MF_STRING, elem->xId, elem->xName))
					err = true;
			}
		}
	}

	if(err){
		Destroy();
		return false;
	}

	isCreated = true;

	return true;
}

bool MPopupMenu::IsCreated(void)
{
	return isCreated;
}

void MPopupMenu::Destroy(void)
{
	if(isCreated){
		popupMenu.DestroyMenu();
		isCreated = false;
	}
}

//---------------------------------------------------------------------------

bool MPopupMenu::EnableMenuItem(UINT idIn)
{
	MENUITEMINFO menuItemInfo;
	BOOL res;

	menuItemInfo.cbSize = sizeof(MENUITEMINFO);
	menuItemInfo.fMask  = MIIM_STATE;
	menuItemInfo.fState = MFS_ENABLED;

	res = popupMenu.SetMenuItemInfo(idIn, &menuItemInfo, false);

	return res ? true : false;
}

bool MPopupMenu::DisableMenuItem(UINT idIn)
{
	MENUITEMINFO menuItemInfo;
	BOOL res;

	menuItemInfo.cbSize = sizeof(MENUITEMINFO);
	menuItemInfo.fMask  = MIIM_STATE;
	menuItemInfo.fState = MFS_DISABLED;

	res = popupMenu.SetMenuItemInfo(idIn, &menuItemInfo, false);

	return res ? true : false;
}

//---------------------------------------------------------------------------

bool MPopupMenu::Show(CWnd *parentWndIn, int xIn, int yIn)
{
	if(!isCreated) return false;

	return popupMenu.TrackPopupMenuEx(TPM_LEFTALIGN | TPM_LEFTBUTTON, xIn, yIn, parentWndIn, NULL) ? true : false;
}

//---------------------------------------------------------------------------
